import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { EquipMCarbonType } from "@/api/backend/index";
import { ElNotification } from "element-plus"; //处理导航信息
import qs from "qs";

export const useAlarm = defineStore(Names.alarm, {
  state: () => {
    return {
      id: 0, //需要查询的事件id
      single: {}, //单个事件的列表
      carbonChangeList: <any>[],
    };
  },
  getters: {},
  actions: {
    //获取设备事件数据列表
    getAlarmsList(filter: any) {
      return new Promise((resolve, reject) => {
        _get(
          EquipMCarbonType +
            "/list?" +
            qs.stringify(filter, { arrayFormat: "indices" }),
          {},
          "json",
          ""
        )
          .then((res) => {
            console.log(res);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //新增设备事件数据
    addAlarms(data: anyObj) {
      return new Promise((resolve, reject) => {
        _post(EquipMCarbonType, data, "json", "")
          .then((res: any) => {
            console.log(res);
            resolve(res);
            ElNotification({
              type: "success",
              message: res.data.msg,
            });
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //编辑设备事件数据
    editAlarms(data: anyObj) {
      return new Promise((resolve, reject) => {
        _put(EquipMCarbonType, data, "json", "")
          .then((res: any) => {
            console.log(res);
            resolve(res);
            ElNotification({
              type: "success",
              message: res.data.msg,
            });
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //删除设备事件数据
    deleteAlarms(data: anyObj) {
      return new Promise((resolve, reject) => {
        _delete(EquipMCarbonType + `/${data.id}`, {}, "json", "")
          .then((res: any) => {
            resolve(res);
            ElNotification({
              type: "success",
              message: res.data.msg,
            });
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
  },
});
